package com.haojii.jdo;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

import javax.jdo.PersistenceManager;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.Key;
import com.haojii.jdo.orm.unowned_one2many.Food;
import com.haojii.jdo.orm.unowned_one2many.Person;
import com.haojii.jdo.utils.PMF;

@SuppressWarnings("serial")
public class JdoServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		resp.setContentType("text/plain");
		PersistenceManager pm = PMF.get().getPersistenceManager();
		
		Person p = new Person();
		p.setAge(5);
		p.setName("HAO");
		
		Food f1 = new Food();
		f1.setName("f1");
		Food f2 = new Food();
		f2.setName("f2");
		pm.makePersistent(f1);
		pm.makePersistent(f2);
		
		System.out.println(f1.getKey());
		System.out.println(f2.getKey());
		Set<Key> foodSet = new HashSet<Key>();
		foodSet.add(f1.getKey());
		foodSet.add(f2.getKey());
		
		p.setFavoriteFoods(foodSet);
		
		pm.makePersistent(p);
		System.out.println(p.getKey());
		resp.getWriter().println("Hello, world");
	}
}
